layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
   window.layedit = layui.layedit;

    //获取路径参数
    var request = GetRequest();

    $.get("/produce/" + request.produceId,function (data) {
        $("#id").val(data.id);
        $("#originInformation").val(data.originInformation);
        $("#adoptStandard").val(data.adoptStandard);
        $("#qualityInspectionReport").val(data.qualityInspectionReport);
        $("#certificateOfQuality").val(data.certificateOfQuality);
        $("#productLicense").val(data.productLicense);

        //采用标准
        if (data.adoptStandard != null && data.adoptStandard != "") {
            var picArr = data.adoptStandard.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg1').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //质检报告
        if (data.qualityInspectionReport != null && data.qualityInspectionReport != "") {
            var picArr = data.qualityInspectionReport.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg2').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //质量管理体系认证书
        if (data.certificateOfQuality != null && data.certificateOfQuality != "") {
            var picArr = data.certificateOfQuality.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg3').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //产品许可证
        if (data.productLicense != null && data.productLicense != "") {
            var picArr = data.productLicense.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg4').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

    });

    //采用标准图片上传
    var flag1 = true;
    $("#uploadImg1").click(function () {
        if (flag1) {
            flag1 = false;
            $('#showImg1').html('');
            $("#adoptStandard").val('');
        }
    });
    upload.render({
        elem: '#uploadImg1'
        ,url: '/upload/pic'
        // ,multiple: true
        ,field:"uploadFile"
        ,before: function(obj){
        }
        ,done: function(res){
            //上传完毕
            var url = res.url;
            /*$('#showImg').html('');*/
            $('#showImg1').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#adoptStandard").val($("#adoptStandard").val() + url + ";");
        }
    });

    //质检报告图片上传
    var flag2 = true;
    $("#uploadImg2").click(function () {
        if (flag2) {
            flag2 = false;
            $('#showImg2').html('');
            $("#qualityInspectionReport").val('');
        }
    });
    upload.render({
        elem: '#uploadImg2'
        ,url: '/upload/pic'
        // ,multiple: true
        ,field:"uploadFile"
        ,before: function(obj){
        }
        ,done: function(res){
            //上传完毕
            var url = res.url;
            /*$('#showImg').html('');*/
            $('#showImg2').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#qualityInspectionReport").val($("#qualityInspectionReport").val() + url + ";");
        }
    });

    //质量管理体系认证书
    var flag3 = true;
    $("#uploadImg3").click(function () {
        if (flag3) {
            flag3 = false;
            $('#showImg3').html('');
            $("#certificateOfQuality").val('');
        }
    });
    upload.render({
        elem: '#uploadImg3'
        ,url: '/upload/pic'
        // ,multiple: true
        ,field:"uploadFile"
        ,before: function(obj){
        }
        ,done: function(res){
            //上传完毕
            var url = res.url;
            /*$('#showImg').html('');*/
            $('#showImg3').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#certificateOfQuality").val($("#certificateOfQuality").val() + url + ";");
        }
    });

    //产品许可证
    var flag4 = true;
    $("#uploadImg4").click(function () {
        if (flag4) {
            flag4 = false;
            $('#showImg4').html('');
            $("#productLicense").val('');
        }
    });
    upload.render({
        elem: '#uploadImg4'
        ,url: '/upload/pic'
        // ,multiple: true
        ,field:"uploadFile"
        ,before: function(obj){
        }
        ,done: function(res){
            //上传完毕
            var url = res.url;
            /*$('#showImg').html('');*/
            $('#showImg4').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#productLicense").val($("#productLicense").val() + url + ";");
        }
    });

    //修改产品信息
    form.on('submit(edit)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //初始化数据
        $.ajax({
            url: '/produce/edit',
            data: data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

